package ywpt.web.action.config;

import java.util.ArrayList;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;

import ywpt.core.SystemMessage;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.ClientType;
import ywpt.service.config.ClientTypeService;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class ClientTypeAction extends BaseAction implements
ModelDriven<ClientType>, Preparable {

	/**
	 * �ͻ�����Action
	 */
	private static final long serialVersionUID = -6367914039491837170L;
	private static final int DEFAULT_PAGE_SIZE = 20;
	private Long id;
	
	
	private ClientType clientType;
	private List<ClientType> clientTypeList = new ArrayList<ClientType>();
	private ClientTypeService  clientTypeService;
	
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public ClientType getClientType() {
		return clientType;
	}
	public void setClientType(ClientType clientType) {
		this.clientType = clientType;
	}
	public List<ClientType> getClientTypeList() {
		return clientTypeList;
	}
	public void setClientTypeList(List<ClientType> clientTypeList) {
		this.clientTypeList = clientTypeList;
	}
	public ClientTypeService getClientTypeService() {
		return clientTypeService;
	}
	public void setClientTypeService(ClientTypeService clientTypeService) {
		this.clientTypeService = clientTypeService;
	}
	//*******************************************************************
	@Override
	public ClientType getModel() {
		return this.clientType;
	}
	@Override
	public void prepare() throws Exception {
		if( id!=null){
			this.clientType = clientTypeService.getClientTypeById(id);
		}else{
			this.clientType = new ClientType();
		}
	}
	// **************************��ݽ�������*******************************
	
	public String list(){
		DetachedCriteria query=DetachedCriteria.forClass(ClientType.class);
		int totalRows=RequestUtils.getTotalRowsFromRequest(getRequest());
		if(totalRows<0)
		{
			totalRows=clientTypeService.getClientTypeCountByCriteria(query);
		}
		int[] rowStartEnd=RequestUtils.getRowStartEnd(getRequest(),
				totalRows,DEFAULT_PAGE_SIZE,0);
		Limit limit=RequestUtils.getLimit(getRequest(),"ec",totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize=limit.getCurrentRowsDisplayed();
		query.addOrder(Order.asc("code"));
		query.setProjection(null);
		clientTypeList.addAll(clientTypeService.getClientTypes(query,pageSize,
				rowStartEnd[0]));
		return SUCCESS;
	}
	
	public String input(){	
		String code = this.clientTypeService.CreateTypeCode();
		clientType.setCode(code);
		return INPUT;
	} 

	public String edit(){
		this.clientType = clientTypeService.getClientTypeById(id);
		return INPUT;
	}
	/**
	 * ����
	 * 
	 * @return
	 */
	public String save() {
		try {
			if (id == null) {
				clientTypeService.saveClientType(clientType);
				this.addActionMessage(SystemMessage.SAVE_SUCCESS);
			} else {
				clientTypeService.updateClientType(clientType);
				this.addActionMessage(SystemMessage.UPDATE_SUCCESS);
				
			}
			return SUCCESS;
		} catch (UniqueConstraintException e) {
			this.addActionError(SystemMessage.SAVE_FAIL + e.getMessage());
			return INPUT;
		}
	}
	
	 public String delete() throws Exception {
		if (id != null) 
		{	
			clientTypeService.removeClientType(id);
			return SUCCESS;				
		}else {
			this.addActionMessage(SystemMessage.NO_OBJECT);
			return INPUT;
		}
	}
	 
	 
	public String select() 
	{
		clientTypeList.addAll(clientTypeService.getClientTypes());
		return SUCCESS;
	}
}
